package com.hx.er.common.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

/**
 * 配置类，可以通过此类获取配置文件中的值。以键值对形式存储。 启动tomcat会自动加载load()方法。运行时这些值一直存储在内存中，以减少磁盘读写率。
 * 
 * @author HX
 */
public class ErProperties {
	private static Properties properties;

	public void load() {
		URL parentPath = Thread.currentThread().getContextClassLoader().getResource("");
		String fullPath = parentPath.getPath() + Constants.PROPERTIES_FILE_PATH;

		FileInputStream fis = null;
		try {
			fis = new FileInputStream(new File(fullPath));
			properties = new Properties();
			properties.load(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void reload() {
		this.load();
	}

	public static String getProperty(String key) {
		return properties.getProperty(key);
	}
	
	public synchronized static void setProperty(String key, String value) {
		properties.setProperty(key, value);
	}
	
	public synchronized void save() {
//		URL parentPath = Thread.currentThread().getContextClassLoader().getResource("");
//		String fullPath = parentPath.getPath() + Constants.PROPERTIES_FILE_PATH;
//		
//		FileInputStream fis = null;
//		try {
//			fis = new FileInputStream(new File(fullPath));
//			properties.store(fis, comments);
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			if (fis != null) {
//				try {
//					fis.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
	}
}
